package controllers;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import model.Goods_tcode_namage;
import model.User_list;
import model.V_goods_tcode_manage;

import com.et.ar.exception.ActiveRecordException;
import com.et.mvc.JsonView;

public class BarCodeController extends ApplicationController {
	/**
	 * 返回所属单位全部保管室
	 */
	public JsonView getBarCodes(int rows, int page) throws Exception {
		User_list ul = (User_list)session.getAttribute("sessionAccount");
		String company_id = ul.getUser_company();
//		String cond = "room_company='"+company_id+"'";
		List<Object> tmpArgs = new ArrayList<Object>();
		Object[] args = tmpArgs.toArray();
		String sql = "SELECT * FROM V_goods_tcode_manage S";
		List<V_goods_tcode_manage> v_goods_tcode_manage=V_goods_tcode_manage.findBySql(V_goods_tcode_manage.class, sql);
		long total = v_goods_tcode_manage.size(); // 查询所属单位全部保管室总数
		List<V_goods_tcode_manage> v_goods_tcode_manage2 = V_goods_tcode_manage.findBySql(V_goods_tcode_manage.class, sql, args, "manage_id desc", rows, (page - 1) * rows);

		// 构造JSON用的数据结构并返回JSON视图
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("total", total);
		result.put("rows", v_goods_tcode_manage2);
		return new JsonView(result);
	}

//	/*
//	 * 根据caseRoomId获取一条记录
//	 */
//	public JsonView getCaseRoomInfo(int id) throws Exception {
//		V_room_shelf v_room_shelf = V_room_shelf.find(V_room_shelf.class, id);
//		return new JsonView(v_room_shelf);
//	}

	/**
	 * 保存新建条形码
	 */
	public JsonView save(Goods_tcode_namage goods_tcode_namage) throws Exception {
		//这里调用的beforSave方法来预先操作
		try{
		//goods_tcode_namage.beforeSave();
		goods_tcode_namage.save();
		}catch(ActiveRecordException e){
			Map<String, Object> result = new HashMap<String, Object>();
			result.put("success", false);
			result.put("msg", "所选单位存在预打印的记录，无法新增记录!");
			return new JsonView(result);
		}
		JsonView view = new JsonView("success:true");
		view.setContentType("text/html;charset=utf-8");
		return view;
	}
	
	/**
	 * 修改条形码
	 */
	public JsonView modify(Goods_tcode_namage goods_tcode_namage) throws Exception {
		String manageId = request.getParameter("manage_id");
		Goods_tcode_namage gtn = Goods_tcode_namage.findFirst(Goods_tcode_namage.class, "manage_id="+manageId, null);
		
		gtn.setTcode_end(Integer.parseInt(goods_tcode_namage.getTcode_end())+1+"");
		gtn.update();
		JsonView view = new JsonView("success:true");
		view.setContentType("text/html;charset=utf-8");
		return view;
	}
	
	/**
	 * 删除条形码
	 */
	public JsonView delCode(Goods_tcode_namage goods_tcode_namage) throws Exception {
		String manageId = request.getParameter("manage_id");
		goods_tcode_namage.setManage_id(manageId);
		goods_tcode_namage.destroy();
		JsonView view = new JsonView("success:true");
		view.setContentType("text/html;charset=utf-8");
		return view;
	}
	
	/**
	 * 更改状态
	 */
	public JsonView chgStstus(int id) throws Exception {
		Goods_tcode_namage goods_tcode_namage = Goods_tcode_namage.find(Goods_tcode_namage.class, id);
		String status = goods_tcode_namage.getStatus();
		String curStatus="250001";
		if("250001".equals(status)){
			curStatus = "250002";
		}if("250002".equals(status)){
			curStatus = "250003";
		}if("250003".equals(status)){
			curStatus = "250004";
			//如果已经确认下发到下属单位，那么就要生成条形码记录到数据库表中
			//goods_tcode_namage.afterUpdate();
		}
		goods_tcode_namage.setStatus(curStatus);
		goods_tcode_namage.update();
		JsonView view = new JsonView("success:true");
		view.setContentType("text/html;charset=utf-8");
		return view;
	}
}
